import axios from 'axios';
import { showFailToast } from 'vant';

// 创建axios实例
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
});
// request拦截器
service.interceptors.request.use(config => {

  return config;
}, error => {
  // Do something with request error
  console.log(error); // for debug
  Promise.reject(error);
});

// respone拦截器
service.interceptors.response.use(
  response => {
    /**
    * res.success 请求状态
    */
    const res = response.data;
    if (res.code == 200) {
      return res.data;
    } else if (res.code == -1) {
      return service.request(response.config);
    } else {
      showFailToast(res.msg || '服务器错误');
      return Promise.reject('error');
    }
  },
  error => {
    console.log('错误：' + error);// for debug
    showFailToast('网络故障');
    return Promise.reject(error);
  }
);


export default service;

export function request(url, method, parameter) {
  return service(url, method, parameter)
}

export function getAction(url, parameter) {
  return service({
    url: url,
    method: 'get',
    params: parameter
  });
}

export function postAction(url, data) {
  return service({
    url: url,
    method: 'post',
    data: data
  });
}